﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class FuLiao_CaiGou_JiHua_MX : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                String strSql1 = "SELECT [ID], [品名]+' '+ [规格]+'('+[单位]+')' as [品名规格] FROM [辅料]";
                SqlDataAdapter sda = new SqlDataAdapter(strSql1, Conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.FuLiao.DataSource = ds;
                this.FuLiao.DataBind();
                GridView_Bounding();
                DataBounding();
            }
           
        }
        public void GridView_Bounding()
        {

            string sql = @"select 辅料采购明细.ID,辅料采购计划.计划名称,品名,规格,单位,计划采购量, 日期 from 辅料采购明细 inner join 辅料采购计划 on 辅料采购计划.ID=辅料采购明细.辅料采购计划ID inner join 辅料 on 辅料.ID=辅料采购明细.辅料ID where 辅料采购明细.辅料采购计划ID like '" + Request.QueryString["ID"] + "'";
            this.SqlDataSource1.SelectCommand = sql;
        }
        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec FuLiao_CaiGou_JiHua_MingXi_Delete " + GridView1.SelectedDataKey.Value.ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
            GridView_Bounding();
        }

        protected void Save_Click(object sender, EventArgs e)
        {
                string 辅料采购计划ID = Request.QueryString["ID"];               
                string 辅料ID = this.FuLiao.Text;              
                string 计划采购量 = this.CaiGouLiang.Text;
                if (float.Parse(计划采购量)<0)
                {
                    this.Message.Text = ".计划采购量不能小于0";
                    return;
                }
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                string strSql = "insert into 辅料采购明细 values('" + 辅料采购计划ID + "','" + 辅料ID + "','" + float.Parse(计划采购量) + "','" + DateTime.Now + "')";
                SqlCommand cmd = new SqlCommand(strSql, Conn);   
                                
                try
                {
                    int i = cmd.ExecuteNonQuery();
                    if (i > 0)
                    {
                        this.Message.Text = ".增加成功";
                    }
                    else
                    {
                        this.Message.Text = ".增加有误，请检查后在添加";
                    }

                }
                catch (Exception ex)
                {
                    this.Message.Text = ex.Message.ToString();
                }
                finally
                {
                    cmd.Clone();
                    Conn.Close();
                }
                GridView_Bounding();
            }
           protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);

        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void FuLiao_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CaiGouLiang.Text = "";
            DataBounding();
        }
        public void DataBounding()
        {
            //清空计划采购量
            this.CaiGouLiang.Text = "";
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            String strsql1 = "select sum(库存) from 辅料 where ID=" + this.FuLiao.Text;
            SqlDataAdapter sda1 = new SqlDataAdapter(strsql1, Conn);
            DataTable dt1 = new DataTable();
            Conn.Open();
            sda1.Fill(dt1);
            this.KuCunLiang.Text = dt1.Rows[0][0].ToString();
            Conn.Close();
            Conn.Dispose();
        }
    }
}
